#pragma once
#include <memory>
#include "tinyrpc/base/buffer.h"
#include "../inetaddress.h"
#include "AbstractChannel.h"
#include "../eventloop.h"
#include "tinyrpc/base/codec/abstractCodec.h"
#include <iostream>
namespace TinyRPC{

 /**
  * SocketChannel 是socket 连接通道
  * SocketChannel 控住 m_sockFd 的开关
  */ 

class SocketChannel;
 // 通过共享指针来传递SocketChannel
using SocketChannelPtr = std::shared_ptr<SocketChannel>;

class SocketChannel : public AbstractChannel{
public:

    int connect(const INetAddress& peerAddress);
    SocketChannel(int fd, const INetAddress& peerAddress);

    /** 读对方的消息,读到自己的缓冲区
     return 读到的长度
     -1 发送错误
    */ 
    int receiveFromRemote();

    // 实际从m_inBuf 里面读取
    // 这里面有一个解码器
    std::string read(AbstractCodec* codec = nullptr);


    /**
     * 写入buffer
     */
    void write(BufferPtr buf);
    void write(const std::string & buffer);
    void write(std::string & buffer, AbstractCodec* codec);

    ~SocketChannel(){
        std::cout<<"SocketChannel del"<<std::endl;
    }

  

public:
    SocketChannel();
    static SocketChannelPtr Create();


private:
    INetAddress m_peerAddress;
    // 接收对手的输入
    BufferPtr m_inBuf;
    // 输出缓冲区
    BufferPtr m_outBuf;
};

}
